package com.zlx.mapper;

import com.zlx.entities.Student;

import java.util.List;
import java.util.Map;

/**
 * @Author 周罗校
 * @Description
 * @ClassName StudentMapperProvider
 * @date 2020.10.10 15:11:25
 * 该版权归周罗校所有，最终解释权以官方为准
 */
public class StudentMapperProvider {
    /**
     * @Author 周罗校
     * @Description 批量添加sql语句拼接
     * @Param [map]
     * @return java.lang.String
     * @Date 2020.10.10 15:21:37
     **/
    public String addBitch(Map<String, List<Student>> map){
        String sql = "insert into student (stu_name,stu_age,stu_password) values";
        List<Student> list = map.get("list");
        StringBuilder sb = new StringBuilder(sql);
        for (int i = 0; i < list.size(); i++) {
            sb.append("(#{list["+i+"].stuName},#{list["+i+"].stuAge},#{list["+i+"].stuPassword})");
            if (i<list.size()-1){
                sb.append(",");
            }
        }
        return sb.toString();
    }
    /**
     * @Author 周罗校
     * @Description 批量删除sql语句拼接
     * @Param [map]
     * @return java.lang.String
     * @Date 2020.10.10 15:22:08
     **/
    public String deleteBitch(Map<String, Integer[]> map){
        String sql = "delete from student where stu_Id in  (";
        Integer[] arrays = map.get("array");
        StringBuilder sb = new StringBuilder(sql);
        for (int i = 0; i < arrays.length; i++) {
            sb.append("#{array["+i+"]}");
            if (i<arrays.length-1){
                sb.append(",");
            }
        }
        sb.append(")");
        return sb.toString();
    }

    public String update(Student student){
        String sql = "update student set ";
        StringBuilder sb = new StringBuilder(sql);
        if (student.getStuName()!=null){
            sb.append("stu_name =#{stuName},");
        }
        if (student.getStuAge()!=null){
            sb.append("stu_age = #{stuAge},");
        }
        if (student.getStuPassword()!=null){
            sb.append("stu_password=#{stuPassword},");
        }
        //删除最后的逗号
        sb.deleteCharAt(sb.length()-1);
        sb.append(" where stu_id = #{stuId}");
        return sb.toString();
    }
}
